package com.lobaby.tools.fx.controller;

import com.lobaby.tools.common.utils.CommandUtils;
import com.lobaby.tools.model.ConsoleResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;

/**
 * @author Administrator
 *         Created by Administrator on 2021/3/2 0002.
 */
@Controller
@RequestMapping("/fileoper")
public class FileOperController {
    private static final Logger LOGGER = LoggerFactory.getLogger(FileOperController.class);

    @RequestMapping("/openfile")
    @ResponseBody
    public String openFile(String type, String path) {
        LOGGER.info("=====================> open path: " + path);
        try {
            if (type.equals("0")) {
                File file = new File(path);

                new Thread(() -> {
                    try {
                        // 打开文件
                        CommandUtils.exeCommand("explorer file:\\\\\\" + path);
//                        Desktop.getDesktop().open(file);
                    } catch (Exception e) {
                        LOGGER.error("", e);
                    }
                }).start();
                return ConsoleResult.successStatus;
            } else {
                new Thread(() -> {
                    try {
                        // 打开文件夹，并定位到该文件
                        CommandUtils.exeCommand("explorer /select," + path);
                    } catch (Exception e) {
                        LOGGER.error("", e);
                    }
                }).start();
                return ConsoleResult.successStatus;
            }
        } catch (Exception e) {
            LOGGER.error("", e);
            return ConsoleResult.faultStatus;
        }
    }
}
